关于python:如何将单列Pandas DataFrame转换为Series | 您所在的位置:网站首页 › python series变成dataframe › 关于python:如何将单列Pandas DataFrame转换为Series |
我有以下数据框: 1234import pandas as pd d = {'gene' : ['foo','bar'],'score' : [4., 3.,]} df = pd.DataFrame(d) df.set_index('gene',inplace=True)哪个使: 12345678In [56]: df Out[56]: score gene foo 4 bar 3 In [58]: type(df) Out[58]: pandas.core.frame.DataFrame我要做的是将其变成系列。 我希望它会返回: 1234gene foo 4 bar 3 #pandas.core.series.Series我试过了,但是没有用: 123456789In [64]: type(df.iloc[0:,]) Out[64]: pandas.core.frame.DataFrame In [65]: df.iloc[0:,] Out[65]: score gene foo 4 bar 3什么是正确的方法? 123456s = df.squeeze() >>> s gene foo 4 bar 3 Name: score, dtype: float64要将其返回到数据框: 12345>>> s.to_frame() score gene foo 4 bar 3 相关讨论 另请参阅stackoverflow.com/questions/33246771/尝试交换括号中的索引: 1df.iloc[:,0]这应该工作。 交换索引可以轻松解决问题: 123456789In [64]: type(df.iloc[0:,]) Out[64]: pandas.core.frame.DataFrame In [65]: df.iloc[[:,0] // Swaped the indices Out[65]: score gene foo 4 bar 3 |
CopyRight 2018-2019 实验室设备网 版权所有 |